//
//  Webview.m
//  悠狸
//
//  Created by dlios on 15-8-30.
//  Copyright (c) 2015年 Dung. All rights reserved.
//

#import "Webview.h"
#import "UMSocial.h"
#import "DB.h"
#import "NovelDataBase.h"
#import "StrategyModel.h"
#import "MyModel.h"
#import "CateModel.h"

@interface Webview ()<UMSocialUIDelegate>
@property (nonatomic, retain)UIWebView *webView;
@end

@implementation Webview
- (void)dealloc
{
    [_model release];
    [_webView release];
    [_url release];
    [_strate release];
    [_url release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"攻略详情";
    self.navigationController.navigationBar.translucent =NO;
 
    self.hidesBottomBarWhenPushed = YES;
    [self create];
}
- (void)create
{
    if (self.strate == nil && self.cate == nil) {
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )];
        [self.view addSubview:_webView];
        _webView.scalesPageToFit = YES;
        [self.webView.scrollView setBackgroundColor:[UIColor whiteColor]];
        NSURL *url = [NSURL URLWithString:self.model.url];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];
        [_webView release];
    }  else if (self.strate == nil && self.model == nil) {
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )];
        [self.view addSubview:_webView];
        _webView.scalesPageToFit = YES;
        [self.webView.scrollView setBackgroundColor:[UIColor whiteColor]];
        NSURL *url = [NSURL URLWithString:self.cate.url];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];
        [_webView release];
    } else {
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )];
        [self.view addSubview:_webView];
        _webView.scalesPageToFit = YES;
        [self.webView.scrollView setBackgroundColor:[UIColor whiteColor]];
        NSURL *url = [NSURL URLWithString:self.strate.url];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];
        [_webView release];
    }

    
    UIView *aview = [[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-120 , self.view.frame.size.width, 60)] autorelease];
    aview.backgroundColor = [UIColor cyanColor];
    aview.userInteractionEnabled = YES;
    [self.view addSubview:aview];
    
    UIButton *abutton = [[[UIButton alloc] initWithFrame:CGRectMake(60*OffWidth, 10*OffHeight, 100*OffWidth, 30*OffHeight)] autorelease];
    [abutton setTitle:@"收藏" forState:UIControlStateNormal];
    [abutton setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
    [aview addSubview:abutton];
    [abutton addTarget:self action:@selector(collection:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    if (_cate != nil) {
        if ([NovelDataBase  selectedMyId:self.cate.Id]) {
            [abutton setTitle:@"已收藏" forState:UIControlStateNormal];
        }
    }
    
    if (_model != nil) {
        if ([NovelDataBase  selectedMyId:self.model.Id]) {
            [abutton setTitle:@"已收藏" forState:UIControlStateNormal];
            
        }
        
    }
    if (_strate != nil) {
        if ([NovelDataBase  selectedMyId:self.strate.Id]) {
            [abutton setTitle:@"已收藏" forState:UIControlStateNormal];
            
        }
    }
    
    UIButton *bbutton = [[[UIButton alloc] initWithFrame:CGRectMake(200*OffWidth, 10*OffHeight, 100*OffWidth, 30*OffHeight)] autorelease];
    [bbutton setTitle:@"分享" forState:UIControlStateNormal];
    [bbutton setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
    [aview addSubview:bbutton];
    [bbutton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
}
//收藏
- (void)collection:(UIButton *)button
{
//      StrategyModel *model = [[StrategyModel alloc] init];
//    model.Id = nil;
//       [NovelDataBase deleteWithMyId:model.Id];
    
//    StrategyModel *model = [[StrategyModel alloc] init];
//    model = self.strate;
////
    
    if (_cate !=nil) {
        BOOL  judge =  [NovelDataBase selectedMyId:self.cate.Id];
        if (judge == YES) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否取消收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [NovelDataBase deleteWithMyId:self.cate.Id];
                [button setTitle:@"收藏" forState:UIControlStateNormal];
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            [alert addAction:ok];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [DB dbOpen];
                
                StrategyModel *model = [[[StrategyModel alloc] initwithld:self.model.Id title:self.model.title likes_count:self.model.likes_count cover_image_url:self.model.cover_image_url url:self.model.url] autorelease];
                //    [NovelDataBase deleteWithMyId:model];
                [NovelDataBase insertWithModel:model];
                [button setTitle:@"已收藏" forState:UIControlStateNormal];
                
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            [alert addAction:ok];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        }

    }
    if (_model !=nil) {
        BOOL  judge =  [NovelDataBase selectedMyId:self.model.Id];
        if (judge == YES) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否取消收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [NovelDataBase deleteWithMyId:self.model.Id];
                [button setTitle:@"收藏" forState:UIControlStateNormal];
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            [alert addAction:ok];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [DB dbOpen];
                
                StrategyModel *model = [[[StrategyModel alloc] initwithld:self.model.Id title:self.model.title likes_count:self.model.likes_count cover_image_url:self.model.cover_image_url url:self.model.url] autorelease];
                //    [NovelDataBase deleteWithMyId:model];
                [NovelDataBase insertWithModel:model];
                [button setTitle:@"已收藏" forState:UIControlStateNormal];
                
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            [alert addAction:ok];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        }
        
    }
    if (_strate !=nil) {
        BOOL  judge =  [NovelDataBase selectedMyId:self.strate.Id];
        if (judge == YES) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否取消收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                
                
                [NovelDataBase deleteWithMyId:self.strate.Id];
                [button setTitle:@"收藏" forState:UIControlStateNormal];
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            [alert addAction:ok];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [DB dbOpen];
                
                StrategyModel *model = [[[StrategyModel alloc] initwithld:self.model.Id title:self.model.title likes_count:self.model.likes_count cover_image_url:self.model.cover_image_url url:self.model.url] autorelease];
                //    [NovelDataBase deleteWithMyId:model];
                [NovelDataBase insertWithModel:model];
                [button setTitle:@"已收藏" forState:UIControlStateNormal];
                
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            [alert addAction:ok];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        }
        
    }

    
    
    
    
}
//分享
- (void)share
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                        appKey:@"55eaf0fbe0f55aa5500090dc"
                                      shareText:self.url
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToTencent, nil]
                                       delegate:self];
}
//隐藏tabbar
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
//显示tabbar
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
@end
